package com.mxfcode.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxfcode.back.mapper.BackAnswerQuestionsMapper;
import com.mxfcode.back.service.BackAnswerQuestionsService;
import com.mxfcode.bbs_common.date.DateUtils;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.BBSAnswerQuestions;
import com.mxfcode.entity.TSUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/4/22 22:06
 */
@Service
public class BackAnswerServiceImp implements BackAnswerQuestionsService {
    private static final Logger logger = LoggerFactory.getLogger(BackAnswerServiceImp.class);

    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private BackAnswerQuestionsMapper backAnswerQuestionsMapper;

    @Override
    public PageDataResult getAnswerQuestionsList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        TSUser user = hostHolder.getUser();
        String roleId = hostHolder.getUser().getRoleId();
        String userId = hostHolder.getUser().getId();
        //判断如果是管理员则查出所有的问答信息
        if("1".equals(roleId)){
            userId = null;
        }
        List<BBSAnswerQuestions> answerQuesList = backAnswerQuestionsMapper.getAnswerQuestionsList(userId);

        PageHelper.startPage(pageNum, pageSize);

        if(answerQuesList.size() != 0){
            PageInfo<BBSAnswerQuestions> pageInfo = new PageInfo<>(answerQuesList);
            pageDataResult.setList(answerQuesList);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }

        return pageDataResult;
    }

    @Override
    public Map<String, Object> addAnswerQuestions(BBSAnswerQuestions answerQuestions) {
        Map<String,Object> data = new HashMap();
        try {
            //默认字段
            answerQuestions.setCreateDate(DateUtils.getCurrentDateToDate());
            answerQuestions.setCreateBy(hostHolder.getUser().getUserName());
            answerQuestions.setCreateName(hostHolder.getUser().getRealName());
            answerQuestions.setUserId(hostHolder.getUser().getId());
            int result = backAnswerQuestionsMapper.insertAnswerQuestions(answerQuestions);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("问答[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("问答[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("问答[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updateAnswerQuestions(BBSAnswerQuestions answerQuestions) {
        Map<String,Object> data = new HashMap();
        try{
            answerQuestions.setUpdateName(hostHolder.getUser().getRealName());
            answerQuestions.setUpdateBy(hostHolder.getUser().getUserName());
            answerQuestions.setUpdateDate(DateUtils.getCurrentDateToDate());
            int result = backAnswerQuestionsMapper.updateAnswerQuestions(answerQuestions);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("问答[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("问答[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("问答[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> del(String id) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除问答
            int result = backAnswerQuestionsMapper.del(id);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除问答失败");
                logger.error("删除问答失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除问答成功");
            logger.info("删除问答成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除问答异常！", e);
        }
        return data;
    }

    @Override
    public BBSAnswerQuestions getAnswerQuestionsById(int id) {
        return backAnswerQuestionsMapper.getAnswerQuestionsById(id);
    }
}
